﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    
    public partial class RepairController : BaseControler
    {

        public ActionResult Summary(DateTime? start_date,DateTime? end_date)
        {
            if (start_date == null || end_date==null)
            {
                ViewBag.StartDate = DateTime.Now.Date.AddYears(-1);
                ViewBag.EndDate = DateTime.Now.Date;

                return View();
            }
            string sql = @"select d.deptname as deptname,count(distinct a.car_no) car_nums,count(*) nums,
                 sum(isnull(a.hour_fee,0)) hour_fee,sum(isnull(a.manage_fee,0)) manage_fee,
                sum(isnull(a.sum_fee,0) - isnull(a.hour_fee,0) - isnull(a.manage_fee,0)) m_fee,
                 sum(isnull(a.sum_fee,0)) as sum_fee,sum(DATEDIFF (day,a.enter_date,a.out_date  )+1) days  from
                 RP_CAR_REPAIR_HEAD a,BS_CAR_RECORD b,BS_EMP c,sys_dept d
                 where a.CAR_NO=b.CAR_NO and a.DRIVER=c.MAN_CODE and a.DEPT_CODE = d.deptcode and
                  convert(varchar(10),a.enter_date,120) between '{0}' and '{1}' and d.use_flag='T'
                 group by d.deptname";

            sql = string.Format(sql, start_date.Value.ToDefaultDateString(), end_date.Value.ToDefaultDateString());
            DataTable table = WebcarDataProvider.DB.ExecuteDataTable(sql);
            ViewBag.StartDate = start_date.Value;
            ViewBag.EndDate = end_date.Value;
            return View(model:table);
        }
        public ActionResult Summary2(string dept,string start_ny, string end_ny,string summaryType)
        {
            if (start_ny == null || end_ny == null)
            {
                ViewBag.StartMonth = DateTime.Now.Date.AddYears(-1).ToString("yyyy-MM");
                ViewBag.EndMonth = DateTime.Now.Date.ToString("yyyy-MM");

                return View();
            }
            DataTable table = null;
            if (summaryType == "1")
            {
                table = this.QueryData(dept, start_ny, end_ny, "e.BASE_NAME");
            }
            if (summaryType == "2")
            {
                table = this.QueryData(dept, start_ny, end_ny, "a.record_no");
            }
            if (summaryType == "3")
            {
                table = this.QueryData(dept, start_ny, end_ny, "c.man_name");
            }

            if (table == null) return View();
            return View(model:table);
        }

        DataTable QueryData(string dept,string sdt,string edt,string group_field)
        {
            string sql = @"select {2} as deptname,count(*) nums,
                 sum(isnull(a.hour_fee,0)) hour_fee,sum(isnull(a.manage_fee,0)) manage_fee,
                sum(isnull(a.sum_fee,0) - isnull(a.hour_fee,0) - isnull(a.manage_fee,0)) m_fee,
                 sum(isnull(a.sum_fee,0)) as sum_fee,sum(DATEDIFF (day,a.enter_date,a.out_date)+1) days  from
                 RP_CAR_REPAIR_HEAD a,BS_CAR_RECORD b,BS_EMP c,sys_dept d,v_repair_kind e
                 where a.CAR_NO=b.CAR_NO and a.DRIVER=c.MAN_CODE and a.DEPT_CODE = d.deptcode and
                  convert(varchar(7),a.enter_date,120) between '{0}' and '{1}' and d.use_flag='T'
                  and a.REPAIR_KIND=e.BASE_CODE and {3}
                 group by {2}";
            string dept_expression=" a.dept_code='"+dept+"'";

            if (dept == "ALL")
            {
                dept_expression = "a.dept_code in (select dept_code from BS_MAN_DEPT where man_code='" + CurrentUser.LoginUserID + "')";
            }
            sql = sql.FormatWith(sdt, edt, group_field, dept_expression);
            return WebcarDataProvider.DB.ExecuteDataTable(sql);
        }
    }
}